<?php
require_once dirname(__FILE__) . '/../includes/common.inc.php';
require_once dirname(__FILE__) . '/../includes/pdoorm.inc.php';

header('Content-Type: text/plain');



  
try {
  $action = MiscUtils::getParam('action', NULL);
  $condition = MiscUtils::getParam('c', 'where 1 = 1');
  $e_oid = MiscUtils::getParam('oid', NULL);
  
    switch($action){
        case'v1user_p':
            pdofind1($action,$condition,$e_oid);
            break;  
            
        default:
             pdofind($action,$condition,$e_oid);
            break;  
        
    }  
  
  
} catch (PDOException $e) {
  echo $e->getMessage();
}


/**
 *function list  
 *2011��8��2�� 14:11:59 ns.lanbinbin 
**/

function pdofind($action,$condition,$e_oid){
	
	if(empty($action)) $action = MiscUtils::getParam('name', NULL);

  $condition .= ($e_oid) ? ' AND u.e_oid = \'' . $e_oid . '\'' : '';
  
  $result = new stdClass();
  $result->data = array();

  
    $sql2 = "SELECT u.e_oid as id, u.* FROM ns_".$action." as u $condition";
    
    $tmp=getall($sql2);
    
   if(!empty($tmp)) {
	    foreach($tmp as $tmp2){
	    	$result->data[] = $tmp2;
	    }
   }else{
//   	$result->data[]='NO RECORD!';
   }
    
  echo json_encode($result); 
	
}

/**
 *function list  
 *2011年8月2日 14:11:59 ns.lanbinbin 
 *$out_method输出的方式，为空默认json_encode($result)输出，不为空返回$result->data[0] 数组
**/

function pdofind1($action,$condition,$e_oid,$out_method=''){

     if (strpos($condition,'where') === false)
     {
         $condition = "where ".$condition;
     }
     
    switch($action)
    {
        case'expense':
            $condition .= ($e_oid) ? ' AND d.payee = '. $e_oid .'': '';
            break;  
            
        case'v1purchaseitem':
            $condition .= ($e_oid) ? ' AND d.sampleId = \'' . $e_oid . '\'' : '';
            break;
        case'v1myorder_p':
            {
              $condition .= ($e_oid) ? ' AND d.trackId = \'' . $e_oid . '\'' : ''; //handledialogssignee 
              $action = 'v1myorder';
            }           
        case'v1user_p':
            {
              //$condition .= ($e_oid) ? ' AND d.pEnabled = \'' . $e_oid . '\'' : ''; //handledialogssignee
              $action = 'v1user';  
            }
        default:
            $condition .= ($e_oid) ? ' AND d.e_oid = \'' . $e_oid . '\'' : '';          
            break;       
     
    }


    switch($action)
    {
        case'ireception':
        case'isource':
        case'iceremony':
        case'iculture': 
            $orderby = 'order by d.name';
            break;

        case'v1user':
        case'v1user_p': 
                        
            $orderby = 'order by d.account';
            break;            
            
        default:
            $orderby = '';          
            break;       
     
    }     
  
  $result = new stdClass();
  $result->data = array();

  
    $sql2 = "SELECT d.e_oid as id, d.* FROM ns_".$action." as d  $condition  $orderby";
    
    $tmp=getall($sql2);

    
    foreach($tmp as $tmp2){
        
        switch($action){
            case'datezone':
                {
                    $tmp2->start = SimpleDate::fromStamp($tmp2->start);
                    $tmp2->end = SimpleDate::fromStamp($tmp2->end);
                }
                break;
                
            default:
                break;  
        }
        
        $result->data[] = $tmp2;
    }
    
    
    if(!empty($out_method)){
        return $result->data[0];   
    }else{
        echo json_encode($result);
    }    
    
}


?>